package com.lmiot.xyclick.Util;

import com.google.zxing.common.StringUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipUtilOld {
    private static final int BUFF_SIZE = 1048576;
    private static boolean stopZipFlag;

    public interface UnZipListener {
        void result(boolean z);
    }

    public interface ZipListener {
        void result(boolean z);
    }

    public static boolean isStopZipFlag() {
        return stopZipFlag;
    }

    public static void setStopZipFlag(boolean z) {
        stopZipFlag = z;
    }

    public static void zipFiles(Collection<File> collection, File file, ZipListener zipListener) {
        try {
            ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file), 1048576));
            Iterator<File> it = collection.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                File next = it.next();
                if (stopZipFlag) {
                    break;
                }
                zipFile(next, zipOutputStream, "");
            }
            zipOutputStream.close();
            if (zipListener == null) {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (zipListener != null) {
                zipListener.result(false);
            }
            if (zipListener == null) {
                return;
            }
        } catch (Throwable th) {
            if (zipListener != null) {
                zipListener.result(true);
            }
            throw th;
        }
        zipListener.result(true);
    }

    public static void zipFiles(Collection<File> collection, File file, String str, ZipListener zipListener) {
        try {
            ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file), 1048576));
            for (File file2 : collection) {
                zipFile(file2, zipOutputStream, "");
            }
            zipOutputStream.setComment(str);
            zipOutputStream.close();
            if (zipListener == null) {
                return;
            }
        } catch (Exception e) {
            zipListener.result(false);
            e.printStackTrace();
            if (zipListener == null) {
                return;
            }
        } catch (Throwable th) {
            if (zipListener != null) {
                zipListener.result(true);
            }
            throw th;
        }
        zipListener.result(true);
    }

    public static void upZipFile(File file, String str, UnZipListener unZipListener) {
        File file2 = new File(str);
        if (!file2.exists()) {
            file2.mkdirs();
        }
        try {
            ZipFile zipFile = new ZipFile(file);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry zipEntry = (ZipEntry) entries.nextElement();
                InputStream inputStream = zipFile.getInputStream(zipEntry);
                File file3 = new File(new String((str + File.separator + zipEntry.getName()).getBytes("8859_1"), StringUtils.GB2312));
                if (!file3.exists()) {
                    File parentFile = file3.getParentFile();
                    if (!parentFile.exists()) {
                        parentFile.mkdirs();
                    }
                    file3.createNewFile();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(file3);
                byte[] bArr = new byte[1048576];
                while (true) {
                    int read = inputStream.read(bArr);
                    if (read <= 0) {
                        break;
                    }
                    fileOutputStream.write(bArr, 0, read);
                }
                inputStream.close();
                fileOutputStream.close();
            }
            if (unZipListener == null) {
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (unZipListener == null) {
                return;
            }
        } catch (Throwable th) {
            if (unZipListener != null) {
                unZipListener.result(true);
            }
            throw th;
        }
        unZipListener.result(true);
    }

    public static boolean upZipFile00(File file, String str) {
        File file2 = new File(str);
        if (!file2.exists()) {
            file2.mkdirs();
        }
        try {
            ZipFile zipFile = new ZipFile(file);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry zipEntry = (ZipEntry) entries.nextElement();
                InputStream inputStream = zipFile.getInputStream(zipEntry);
                File file3 = new File(new String((str + File.separator + zipEntry.getName()).getBytes("8859_1"), StringUtils.GB2312));
                if (!file3.exists()) {
                    File parentFile = file3.getParentFile();
                    if (!parentFile.exists()) {
                        parentFile.mkdirs();
                    }
                    file3.createNewFile();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(file3);
                byte[] bArr = new byte[1048576];
                while (true) {
                    int read = inputStream.read(bArr);
                    if (read <= 0) {
                        break;
                    }
                    fileOutputStream.write(bArr, 0, read);
                }
                inputStream.close();
                fileOutputStream.close();
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } catch (Throwable unused) {
        }
        return true;
    }

    public static ArrayList<File> upZipSelectedFile(File file, String str, String str2) {
        ArrayList<File> arrayList = new ArrayList<>();
        File file2 = new File(str);
        if (!file2.exists()) {
            file2.mkdir();
        }
        try {
            ZipFile zipFile = new ZipFile(file);
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry zipEntry = (ZipEntry) entries.nextElement();
                if (zipEntry.getName().contains(str2)) {
                    InputStream inputStream = zipFile.getInputStream(zipEntry);
                    File file3 = new File(new String((str + File.separator + zipEntry.getName()).getBytes("8859_1"), StringUtils.GB2312));
                    if (!file3.exists()) {
                        File parentFile = file3.getParentFile();
                        if (!parentFile.exists()) {
                            parentFile.mkdirs();
                        }
                        file3.createNewFile();
                    }
                    FileOutputStream fileOutputStream = new FileOutputStream(file3);
                    byte[] bArr = new byte[1048576];
                    while (true) {
                        int read = inputStream.read(bArr);
                        if (read <= 0) {
                            break;
                        }
                        fileOutputStream.write(bArr, 0, read);
                    }
                    inputStream.close();
                    fileOutputStream.close();
                    arrayList.add(file3);
                }
            }
            return arrayList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static ArrayList<String> getEntriesNames(File file) {
        ArrayList<String> arrayList = new ArrayList<>();
        try {
            Enumeration<?> entriesEnumeration = getEntriesEnumeration(file);
            while (entriesEnumeration.hasMoreElements()) {
                arrayList.add(new String(getEntryName((ZipEntry) entriesEnumeration.nextElement()).getBytes(StringUtils.GB2312), "8859_1"));
            }
            return arrayList;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Enumeration<?> getEntriesEnumeration(File file) {
        ZipFile zipFile;
        try {
            zipFile = new ZipFile(file);
        } catch (IOException e) {
            e.printStackTrace();
            zipFile = null;
        }
        return zipFile.entries();
    }

    public static String getEntryComment(ZipEntry zipEntry) {
        try {
            return new String(zipEntry.getComment().getBytes(StringUtils.GB2312), "8859_1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getEntryName(ZipEntry zipEntry) {
        try {
            return new String(zipEntry.getName().getBytes(StringUtils.GB2312), "8859_1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static void zipFile(File file, ZipOutputStream zipOutputStream, String str) {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append(str.trim().length() == 0 ? "" : File.separator);
            sb.append(file.getName());
            String str2 = new String(sb.toString().getBytes("8859_1"), StringUtils.GB2312);
            if (file.isDirectory()) {
                for (File file2 : file.listFiles()) {
                    if (!stopZipFlag) {
                        zipFile(file2, zipOutputStream, str2);
                    } else {
                        return;
                    }
                }
                return;
            }
            byte[] bArr = new byte[1048576];
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file), 1048576);
            zipOutputStream.putNextEntry(new ZipEntry(str2));
            while (true) {
                int read = bufferedInputStream.read(bArr);
                if (read == -1) {
                    break;
                } else if (stopZipFlag) {
                    break;
                } else {
                    zipOutputStream.write(bArr, 0, read);
                }
            }
            bufferedInputStream.close();
            zipOutputStream.flush();
            zipOutputStream.closeEntry();
        } catch (Exception unused) {
        }
    }
}
